package com.fresher.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.fresher.model.Log;
import com.fresher.model.User;

@Repository
public class LogDao {
    @PersistenceContext
    private EntityManager entityManager;
	
	// add log of user
    	@Transactional
		public void addLog(String logDetail, String username) {

			Log lg=new Log();
			

				User us = entityManager.find(User.class,username);
				if (us == null) {
					throw new IllegalArgumentException("Unknown user id");
				}
				lg.setLogDetail(logDetail);
				lg.setUser(us);
				// update
				entityManager.persist(lg);

		}
		
		//view list log
    	@Transactional
		public List<Log> getLogs(){
			
			// Obtains entity manager object


			// -----------Begin transaction-----------
			List<Log> LogsList=null;

				// Get a list of logs from DB
				TypedQuery<Log> query = entityManager
						.createQuery(
								"SELECT l FROM Log l",
								Log.class);
				

				LogsList = (List<Log>) query.getResultList();
				


			// -----------End transaction-----------

			return LogsList;
		}
	
}
